package com.kbbywyclient.ui.activity;

import android.view.View;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.BasicStatus;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.data.model.base.BaseModel;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.SettingModule;
import com.kbbywyclient.presenter.SettingPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.SettingContract;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.download.TDevice;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomDialog;
import com.kbbywyclient.widget.CustomToolBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:
 * Created by FHW
 * on 2017/4/20
 */

public class SettingActivity extends BaseActivity implements SettingContract.View {

    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;

    @BindView(R.id.tv_shop_dot)
    TextView tv_shop_dot;
    @BindView(R.id.tv_setting_dot)
    TextView tv_setting_dot;
    @BindView(R.id.tv_version_dot)
    TextView tv_version_dot;


    @BindView(R.id.tv_sign_out)
    TextView tv_sign_out;
    @BindView(R.id.tv_table_notice)
    TextView tv_table_notice;

    @Inject
    SettingPresenter settingPresenter;

    private int activation = 1;//是否激活

    private String goToType = "";

    private int mType = 0;//0-贷款业务 1-信用卡业务

    private int curVersion = -1;//当前版本

    private String sendInfo = "0";//表单更新通知开关,1开0关

    private CustomDialog dialog;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_setting;
    }

    @OnClick({R.id.tv_sign_out, R.id.rl_basic_info, R.id.rl_shop_setting, R.id.rl_certify, R.id.rl_modify_pwd
            , R.id.rl_recommend, R.id.rl_version, R.id.rl_about_we, R.id.tv_table_notice})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_sign_out:
                if (PreUtils.isLogin(this)) {
                    PreUtils.clearUser(this);
                    finish();
                }
                break;
            case R.id.rl_basic_info:
                HashMap<String, Object> map1 = new HashMap<>();
                map1.put("activation", activation);
                ActivitySkipUtil.skipAnotherActivity(this, ModifyInfoActivity.class, map1);
                break;
            case R.id.rl_shop_setting://店铺设置
                ActivitySkipUtil.skipAnotherActivity(this, StoreSettingActivity.class);
                break;
            case R.id.rl_certify://认证
                HashMap<String, Object> map = new HashMap<>();
                map.put("mType", mType);
                if (activation == 1) {
                    map.put("activation", activation);
                    ActivitySkipUtil.skipAnotherActivity(this, InfoCertifySucActivity.class, map);
                } else {
//                    if (goToType.equals("1")) {//未设置资料
//                        showInfoDialog();
//                    } else if (goToType.equals("2")) {//未设置过资质认证
                    goToInfoCertifyView(map);
//                    } else {
//                        if (activation == 3) {//未通过，重新认证
//                            goToInfoCertifyView(map);
//                        } else {
//                            ActivitySkipUtil.skipAnotherActivity(this, InfoCertifySucActivity.class, map);
//                        }
//                    }
                }
                break;
            case R.id.rl_modify_pwd:
                ActivitySkipUtil.skipAnotherActivity(this, PwdModityActivity.class);
                break;
            case R.id.tv_table_notice:
                switch (sendInfo) {
                    case "0":
                        settingPresenter.getModifyTableNotice(PreUtils.getUser(this).getSalesID() + "", "1");
                        break;
                    case "1":
                        settingPresenter.getModifyTableNotice(PreUtils.getUser(this).getSalesID() + "", "0");
                        break;
                }
                break;
            case R.id.rl_recommend://推荐给朋友
                ActivitySkipUtil.skipAnotherActivity(this, RecommendScoreActivity.class);
                break;
            case R.id.rl_version:
                ToastUtil.showShort("检查版本");
                break;
            case R.id.rl_about_we:
                ActivitySkipUtil.skipAnotherActivity(this, AboutMeActivity.class);
                break;
        }
    }

    private void goToInfoCertifyView(HashMap<String, Object> map) {
        switch (mType) {
            case 0:
                ActivitySkipUtil.skipAnotherActivity(this, InfoCertifyLoanActivity.class, map);
                break;
            case 1:
                ActivitySkipUtil.skipAnotherActivity(this, InfoCertifyCreditActivity.class, map);
                break;
        }
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);

        activation = getIntent().getIntExtra("activation", -1);
        mType = getIntent().getIntExtra("mType", -1);
        curVersion = getIntent().getIntExtra("curVersion", -1);
        goToType = getIntent().getStringExtra("goToType");

        v_toolbar.setLeftViewOnClickLinster(v -> finish());

    }

    @Override
    public void initData() {
//        settingPresenter.getBasicStatus(PreUtils.getUser(this).getSalesID() + "");
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("设置");
        settingPresenter.getBasicStatus(PreUtils.getUser(this).getSalesID() + "");
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getSettingComponent(new SettingModule(this)).inject(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 回调
     *
     * @param settingUpdate
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.SettingUpdate settingUpdate) {
        goToType = settingUpdate.getGoToType();
    }

    /**
     * 去填写资料
     */
    private void showInfoDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "您未设置基本资料，请先设置基本资料"
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                ActivitySkipUtil.skipAnotherActivity(SettingActivity.this, ModifyInfoActivity.class);
                dialog.dismiss();
            }
        }, getString(R.string.cancel), "设置基本资料");
        dialog.show();
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadSucStatus(BasicStatus basicStatus) {
        showRedDotView(basicStatus.getShopStatus(), tv_shop_dot);
        switch (mType) {
            case 0:
                showRedDotView(basicStatus.getLoansStatus(), tv_setting_dot);
                break;
            case 1:
                showRedDotView(basicStatus.getCreditCardStatus(), tv_setting_dot);
                break;
        }
        sendInfo = basicStatus.getNoticeStatus() + "";
        switch (sendInfo) {
            case "0":
                tv_table_notice.setBackgroundResource(R.mipmap.icon_switch_table_update_n);
                break;
            case "1":
                tv_table_notice.setBackgroundResource(R.mipmap.icon_switch_table_update_s);
                break;
        }

        if (TDevice.getVersionCode() < curVersion) {//新版本
            tv_version_dot.setVisibility(View.VISIBLE);
        } else {
            tv_version_dot.setVisibility(View.GONE);
        }

    }

    private void showRedDotView(int num, View view) {
        switch (num) {
            case 0:
                view.setVisibility(View.INVISIBLE);
                break;
            case 1:
                view.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void loadFaiStatus(String errorMsg) {

    }

    @Override
    public void loadSucNotice(BaseModel baseModel) {
        switch (sendInfo) {
            case "0":
                sendInfo = "1";
                tv_table_notice.setBackgroundResource(R.mipmap.icon_switch_table_update_s);
                break;
            case "1":
                sendInfo = "0";
                tv_table_notice.setBackgroundResource(R.mipmap.icon_switch_table_update_n);
                break;
        }
    }

    @Override
    public void loadFaiNotice(String errorMsg) {

    }
}
